package com.uenpay.im.ui.old.activity.movement;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.weight.listview.RefreshListView;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-23 下午7:08:33
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class PersonalMovementActivity extends BaseActivity implements
        RefreshListView.IRefreshListViewListener {
    private int page = 1;
    private MovementHandler movementHandler = new MovementHandler();
    private RefreshListView listview;
    private MovementListAdapter adapter = null;
    private String cust_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_xlist_layout;
    }

    @Override
    public void initView(View view) {
        cust_id = getIntent().getStringExtra("cust_id");
        initView();
        initListener();
        getData();
    }

    protected void initView() {
        listview = (RefreshListView) findViewById(R.id.discover_listview);
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.movement);
    }

    protected void initListener() {
        listview.setPullLoadEnable(true);
        listview.setRefreshListViewListener(this);
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                MovementEntity entity = (MovementEntity) arg0.getAdapter()
                        .getItem(arg2);
                if (entity != null) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("movementId", entity.movementId);
                    ActivityHandle.startActivity(PersonalMovementActivity.this, MovementDetailActivity.class, bundle, -1);
                }
            }
        });
    }

    private void getData() {
        movementHandler.getMovementList(2, page, cust_id,
                new MovementHandler.IGetMovementListResultListener() {

                    @Override
                    public void onResult(int status, String msg,
                                         ArrayList<MovementEntity> arrayList) {
                        onLoad();
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            if (page == 1) {
                                if (adapter == null) {
                                    adapter = new MovementListAdapter(
                                            PersonalMovementActivity.this,
                                            arrayList);
                                } else {
                                    adapter.setData(arrayList);
                                }
                                listview.setAdapter(adapter);
                                listview.setPullLoadEnable(true);
                            } else {
                                if (arrayList != null && arrayList.size() > 0) {
                                    adapter.addData(arrayList);
                                } else {
                                    listview.setPullLoadEnable(false);
                                }
                            }
                        }
                    }
                });
    }

    private void onLoad() {
        listview.stopRefresh();
        listview.stopLoadMore();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
